package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.BannerListInDTO;
import cn.shengongying.admin.dto.JobIdentityPictureInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.model.JobIdentityPicture;
import cn.shengongying.service.IJobIdentityPictureService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author: RXK
 * date: 2022/2/19 星期六
 * desc:
 */
@Controller
@RequestMapping("/sys/job/identity/picture")
public class JobIdentityPictureController extends BaseController{

    private final IJobIdentityPictureService jobIdentityPictureService;

    public JobIdentityPictureController(IJobIdentityPictureService jobIdentityPictureService) {
        this.jobIdentityPictureService = jobIdentityPictureService;
    }


    @RequestMapping("/index")
    public String index() {
        return "/job_identity_picture/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<JobIdentityPicture>> list(@RequestBody BannerListInDTO inDTO) {
        List<JobIdentityPicture> list = jobIdentityPictureService.list();
        inDTO.setData(list);
        long count = jobIdentityPictureService.count();
        inDTO.setRecordsTotal(count);
        return inDTO;
    }



    @ResponseBody
    @RequestMapping("/save")
    public ResponseResult<String> save(JobIdentityPictureInDTO inDTO, HttpServletRequest request) {
        JobIdentityPicture jobIdentityPicture = inDTO.bannerList();
        String file;
        try {
            file = multiFile(request);
        } catch (Exception e) {
            logger.error("上传图片出现异常",e);
            return ResponseResult.fail();
        }
        jobIdentityPicture.setIdentityPicture(file);
        jobIdentityPictureService.saveOrUpdate(jobIdentityPicture);
        return ResponseResult.success();
    }


    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult<String> delete(@RequestBody JobIdentityPictureInDTO inDTO) {
        jobIdentityPictureService.removeById(inDTO.getId());
        return ResponseResult.success();
    }



}
